import { $Message } from '../../../components/dist/base/index'
Page({
  data: {
    selectIndex: 0,
    isShowMore: true,
    uploadText: '上传文件',
    buttonList: [
      { text: '提交' },
      { text: '预约记录' }
    ],
    terminalList: [
      {
        serial: '001',
        paper: 123,
        address: '图书馆131231图书馆131231图书馆131231图书馆131231图书馆131231',
      },
      {
        serial: '002',
        paper: 23,
        address: '图书馆131231',
      },
      {
        serial: '003',
        paper: 23,
        address: '图书馆131231',
      },
      {
        serial: '004',
        paper: 23,
        address: '图书馆131231',
      },
      {
        serial: '005',
        paper: 23,
        address: '图书馆131231',
      },
      {
        serial: '006',
        paper: 23,
        address: '图书馆131231'
      }
    ],
    tempFiles: []
  },
  onLoad() {
  },
  menuClick(e) {
    this.setData({
      selectIndex: e.currentTarget.dataset.item.serial
    })
  },
  onButton (event) {
    const { index } = event.detail
    switch (index) {
      case 0:
        console.log('点击了第一个按钮')
        break
      case 1:
        console.log('点击了第二个按钮')
        break
    }
    // wx.$API['common/getNotice']({}).then((res) => {
    // })
  },
  error (e) {
    console.log(e)
    wx.showModal({
      title: '提示',
      content: '您已关闭摄像头，是否前去设置?',
      success(res) {
        if (res.confirm) {
          wx.openSetting({
            success(res) {
              setTimeout(() => {
                wx.redirectTo({
                  url: '/pages/workService/appointPrint/index'
                })
              }, 500)
              console.log(res.authSetting)
            },
            fail(err) {
              console.log(err)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  more() {
    this.data.terminalList.push({
      serial: '006',
      paper: 23,
      address: '图书馆131231'
    })
    this.setData({
      isShowMore: false,
      terminalList: this.data.terminalList
    })
  },
  refreshTempFiles(res) {
    const newTempFiles = res.tempFiles
    const oldTempFiles = this.data.tempFiles
    const allTempFiles = [].concat(oldTempFiles, newTempFiles)
    const listTempFiles = []
    let times
    let times1
    const filterTempFiles = allTempFiles.filter((item) => {
      const mate = item.name.replace(/.+\./, '')
      if (mate === 'pdf') {
        return true
      } else {
        clearTimeout(times1)
        times1 = setTimeout(() => {
          $Message({
            content: '非pdf格式已删除',
            type: 'warning',
            duration: 5
          })
        }, 500)
      }
    })
    for (const file of filterTempFiles) {
      let flag = true
      for (const item of listTempFiles) {
        if (file.name === item.name && file.size === item.size) {
          clearTimeout(times)
          times = setTimeout(() => {
            wx.showToast({
              title: '重复文件已覆盖',
              icon: 'none',
              duration: 2000
            })
          }, 500)
          flag = false
          break
        }
      }
      if (flag) {
        listTempFiles.push(file)
      }
    }
    this.setData({
      tempFiles: listTempFiles,
      uploadText: listTempFiles.length ? '继续上传' : '上传文件'
    })
  },
  ceshi() {
    wx.chooseMessageFile({
      count: 5,
      type: 'file',
      success: (res) => {
        this.refreshTempFiles(res)
      }
    })
  }
})